package com.ma.chapter4.t7;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName TestReentrantLock
 * @Author: mayongqiang
 * @DATE 2022/3/14 20:40
 * @Description: 可重入锁
 */
@Slf4j(topic = "m.TestReentrantLock")
public class TestReentrantLock1 {
    static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        method1();
    }

    private static void method1() {
        //获取锁
        lock.lock();
        try {
            //临界代码
            log.debug("execute method1");
            method2();
        } finally {
            //释放锁
            lock.unlock();
        }
    }

    private static void method2() {
        lock.lock();
        try {
            log.debug("execute method2");
            method3();
        } finally {
            lock.unlock();
        }
    }

    private static void method3() {
        lock.lock();
        try {
            log.debug("execute method3");
        } finally {
            lock.unlock();
        }

    }

}
